<?php
namespace app\member\controller;
class Index extends Base
{
    public function index(){
        $userdetail = getSession("user");
        $this->assign("userdetail",$userdetail);
        $member = db('member')->field("birthmonth,birthday,balance")->where(['jobnumber'=>$userdetail['userid'],'telphone'=>$userdetail['mobile']])->find();
        if (empty($member)){
            $this->assign("ismember",false);
            $this->assign("member",['balance'=>0]);
             
        }else{
            $this->assign("ismember",true);
            $this->assign("member",$member);
        }
    
        //dump($members);
        return $this->fetch();
        //return $this->fetch();
    }
    
    public function consumption(){
        $userdetail = getSession("user");
        if (request()->isPost()){
            $data = input();
            //查询余额
            $amount = trim($data['amount']);
            if (!is_numeric($amount)){
                $this->error("请输入正确的消费金额！");
                exit();
            }
            $member = db('member')->where(['jobnumber'=>$userdetail['userid'],'telphone'=>$userdetail['mobile']])->field('balance')->find();
            if ($amount>$member['balance']){
                $this->error("余额不足！",url('Index/index'));
            }else{
                //需要加事务
                $shop = db("member_shop")->where(['id'=>$data['shopid']])->find();
                //减余额
                $res = $this->reduce($userdetail['userid'], $amount);
                //增加店铺营业额
                $res2 = $this->income($data['shopid'], $amount);
                //添加消费记录
                $res3 = $this->addRecord($userdetail['userid'], $userdetail['name'], $userdetail['mobile'], $shop['id'], $shop['name'], $amount, $data['remark']);
                
                if ($res&&$res2&&$res3){
                    $this->success("消费支付成功！",url('Index/index'));
                }else{
                    $this->error("支付失败，请联系管理员！");
                }
            }
        }
        
         $shop_tag = input('shop');
         if (!isset($shop_tag)){
             $this->error("请通过扫描店铺二维码进行消费！");
         }else{
             $shop = db("member_shop")->where(['ewm_content'=>$shop_tag])->find();
             if (empty($shop)){
                 $this->error("店铺信息不正确，请扫描店铺二维码进行消费！");
             }else{
                 $this->assign("shop",$shop);
             }
         }
         return $this->fetch();
    }
    /**
     * 选店铺消费
     */
    public function choseCons(){
        $this->assign("user",getSession("user"));
        $shops = db("member_shop")->where(['is_del'=>0])->select();
        if (empty($shops)){
            $this->error("店铺信息不正确，请扫描店铺二维码进行消费！");
        }else{
            $this->assign("shops",$shops);
        }
        return $this->fetch();
    }
    
    /**
     * 消费记录
     */
    public function consRecord(){
        //echo "消费记录";
        //查询消费记录
       
        
        $records = db("member_dec")->where(['jobnumber'=>input('userid')])->select();
        $this->assign("records",$records);
        return $this->fetch("pay_his");
    }
    /**
     * 减余额操作
     * @param unknown $jobnumber
     * @param unknown $amount
     */
    private function reduce($jobnumber,$amount){
        $res = db('member')->where(['jobnumber'=>$jobnumber])->setDec('balance',$amount);
        return $res;
    }
    
    private function income($shopid,$amount){
        $res = db("member_shop")->where(['id'=>$shopid])->setInc("income",$amount);
        return $res;
    }
    
    private function addRecord($jobnumber,$name,$telphone,$shopid,$shopname,$fee,$remark){
        $data=[
           'jobnumber'=>$jobnumber,
            'name'=>$name,
            'telphone'=>$telphone,
            'shopid'=>$shopid,
            'shopname'=>$shopname,
            'fee'=>$fee,
            'remark'=>$remark,
            'create_time'=>time()
        ];
        $res = db('member_dec')->insert($data);
        return $res;
    }
    
    
}
